package com.allwees.bs.module.pms.service.impl;

import com.allwees.bs.core.model.constant.CacheName;
import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.pms.dao.CategoryDao;
import com.allwees.bs.module.pms.dto.CategoryDto;
import com.allwees.bs.module.pms.entity.Category;
import com.allwees.bs.module.pms.service.CategoryService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author yangylsky
 * @since 2019-08-21
 */
@Service
public class CategoryServiceImpl extends BoServiceImpl<CategoryDao, Category> implements CategoryService {

    @Cacheable(cacheNames = CacheName.APP_CATEGORY,key = "#categoryNo")
    @Override
    public String getFullname(String categoryNo) {
        String name = "";
        Category category = baseMapper.selectByNo(categoryNo);
        if (Objects.nonNull(category)) {
            name = category.getName();
            if (!category.isTop()) {
                name = name + "/" + getFullname(category.getParentNo());
            }
        }
        return name;
    }

    @CacheEvict(cacheNames = CacheName.APP_CATEGORY,allEntries = true)
    @Override
    public boolean sort(CategoryDto dto){
        Category category = new Category();
        category.setUuid(dto.getUuid());
        category.setIdx(dto.getIdx());
        return this.updateByUuid(category);
    }
}
